package com.chixing.controller;


import com.chixing.entity.Member;
import com.chixing.service.IMemberService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@RestController
@RequestMapping("/member")
@CrossOrigin
public class MemberController {
    @Autowired
    private IMemberService memberService;
    /**
     * 根据 userId 查询会员信息
     */
    @GetMapping("/{userId}")
    public Result getMemberInfo(@PathVariable Integer userId) {
        Member member = memberService.getMemberByUserId(userId);
        return member != null ? Result.getSuccess(member) : Result.error("未找到该会员");
    }

    /**
     * 支付成功 -> 增加积分（由订单或交易模块调用）
     */
    @PostMapping("/add-points")
    public Result addPoints(@RequestParam Integer userId, @RequestParam Double price) {
        memberService.addPointsOnPaymentSuccess(userId, price);
        return Result.getSuccess("积分增加成功");
    }

    /**
     * 退票成功 -> 扣减积分（由订单模块调用）
     */
    @PostMapping("/deduct-points")
    public Result deductPoints(@RequestParam Integer userId, @RequestParam Double price) {
        memberService.deductPointsOnRefund(userId, price);
        return Result.getSuccess("积分扣减成功");
    }
}
